package com.zjj.bams.entity;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class LoanCreditAccount extends CreditAccount implements Loanable{

    //记录贷款金额
    private long loanAmount;

    /**
     * 贷款
     * @param money  贷款金额
     * @return
     */
    @Override
    public Account requestLoan(double money) {
        if(money<=0){
            System.out.println("贷款金额必须>0");
            return this;
        }
        this.loanAmount+=money;
        this.setBalance(this.getBalance() + money);
        return this;
    }

    /**
     * 还贷
     * @param money  还贷款金额
     * @return
     *
     * balance 10000   ceiling 1500  loanAmount 100000  还贷款金额  money  5000
     * -->  balance 5000   ceiling 1500  loanAmount 95000
     *
     * balance 5000   ceiling 1500  loanAmount 100000  还贷款金额  money  6000
     * -->  balance 0   ceiling 500  loanAmount 94000
     *
     * balance 5000   ceiling 1500  loanAmount 100000  还贷款金额  money  8000
     * -->  balance 5000   ceiling 1500  loanAmount 100000  还款失败
     */
    @Override
    public Account payLoan(double money) {
        if(money<=0){
            System.out.println("还款金额必须>0");
            return this;
        }
        if(money>loanAmount){
            System.out.println("太笨了，还多了！还款金额<=贷款金额");
            return this;
        }
        //账户余额 + 透支额度
        if(this.getBalance() + this.getCeiling() >= money){
            if(this.getBalance()>=money){
                //账户余额 足够支付
                this.loanAmount-=money;
                this.setBalance(this.getBalance() - money);
            }else{
                //账户余额+透支额度 足够支付
                this.loanAmount-=money;
                //先考虑透支额度
                this.setCeiling(this.getCeiling() - (money - this.getBalance()));
                //再考虑账户余额
                this.setBalance(0.0);
            }
        }else {
            System.out.println("您的余额不够支付还款操作！");
        }
        return this;
    }

    public LoanCreditAccount() {

    }

    public LoanCreditAccount(String password, String name, String personId, String email, double balance, double ceiling, long loanAmount) {
        super(password, name, personId, email, balance, ceiling);
        this.loanAmount = loanAmount;
    }

    public LoanCreditAccount(Long id, String password, String name, String personId, String email, double balance, double ceiling, long loanAmount) {
        super(id, password, name, personId, email, balance, ceiling);
        this.loanAmount = loanAmount;
    }

    public long getLoanAmount() {
        return loanAmount;
    }

    public void setLoanAmount(long loanAmount) {
        this.loanAmount = loanAmount;
    }

    @Override
    public String toString() {
        return "LoanCreditAccount{" +
                "loanAmount=" + loanAmount +
                "} " + super.toString();
    }
}
